#include "lw_include.h"
#include "lw_new.h"

typedef void (*init_fun)(void);

klass_info* klass_of(void* self)
{
	LW_ASSERT(self);
	return *((klass_info**)self);
}

size_t size_of(void* self)
{
	LW_ASSERT(self);
	klass_info* kls = *((klass_info**)self);
	return kls->size;
}

void* lw_new(void* klass)
{
	LW_ASSERT(klass);
	klass_info* kls = KLASS(klass);
	if(kls->init)
	{
		((init_fun)kls->init)();
	}
	
	void* p = malloc(kls->size);
	LW_ASSERT(p);
	*((klass_info**)p) = kls;
	return ((voidf)(kls->ctor))(p);
}

void lw_delete(void* self)
{
	if(self)
	{
		klass_info* kls = *((klass_info**)self);
		free(((voidf)(kls->dtor))(self));
	}
}